package com.fuxi.cloud.dyndb;

import cn.hutool.core.util.ObjectUtil;
import com.fuxi.cloud.common.util.JimuSpringContextUtils;
import com.fuxi.cloud.desreport.service.IJmReportDbSourceService;
import com.fuxi.cloud.dyndb.vo.JmreportDynamicDataSourceVo;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * JmreportDataSourceCachePool
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */

public class JmreportDataSourceCachePool {
    private static Map<String, DataSource> dbSources = new HashMap();
    private static Map<String, JmreportDynamicDataSourceVo> dynamicDataSourceVoHashMap = new HashMap();

    public JmreportDataSourceCachePool() {
    }

    public static JmreportDynamicDataSourceVo getCacheDynamicDataSourceModel(String dbKey) {
        JmreportDynamicDataSourceVo var1 = (JmreportDynamicDataSourceVo)dynamicDataSourceVoHashMap.get(dbKey);
        if (ObjectUtil.isNotEmpty(var1)) {
            return var1;
        } else {
            IJmReportDbSourceService var2 = (IJmReportDbSourceService) JimuSpringContextUtils.getBean(IJmReportDbSourceService.class);
            JmreportDynamicDataSourceVo var3 = var2.getByDbKey(dbKey);
            if (var3 != null) {
                dynamicDataSourceVoHashMap.put(dbKey, var3);
            }

            return var3;
        }
    }

    public static DataSource getCacheBasicDataSource(String dbKey) {
        return (DataSource)dbSources.get(dbKey);
    }

    public static void putCacheBasicDataSource(String dbKey, DataSource db) {
        dbSources.put(dbKey, db);
    }
}

